package com.sghd.fight.module.game.event;

import com.sghd.common.event.AbstractReceiver;
import com.sghd.fight.module.game.service.GameService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class DisconnectEventReceiver extends AbstractReceiver<DisconnectEvent> {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private GameService gameService;

    @Override
    public void doEvent(final DisconnectEvent event) {
        logger.debug("[{}]玩家[{}]离线！",event.getCause(), event.getPlayer());
        gameService.offline(event.getPlayer());
        cancelMatch(event);
        //TODO
    }

    /**
     * 取消匹配
     * @param event
     */
    private void cancelMatch(DisconnectEvent event) {
        try {
            gameService.cancelMatch(event.getPlayer());
        } catch (Exception e) {
            logger.error("玩家[{}]取消匹配发生未知错误！", event.getPlayer());
        }
    }

    @Override
    public String[] getEventNames() {
        return new String[]{DisconnectEvent.NAME};
    }

}
